package zuoye7;

import zuoye6.ServerThread;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class MultiTalkServer {
    public static List<Socket> socketList=new ArrayList<>();//建立Socket数组，这里简单编写，用静态数组
    public static void main(String args[]) throws IOException {
        ServerSocket serverSocket=null;
        boolean listening=true;
        try{
            //创建一个ServerSocket在端口4700监听客户请求
            serverSocket=new ServerSocket(4700);
        }catch(IOException e) {
            System.out.println("Could not listen on port:4700.");
            //出错，打印出错信息
            System.exit(-1); //退出
        }
        System.out.println("开始连接客户端......");
        while(listening){ //循环监听
            //监听到客户请求，根据得到的Socket对象和客户计数创建服务线程，并启动之
            Socket s = serverSocket.accept();//保存监听到的客户端，这步很重要
            socketList.add(s);
            new ServerThread1(s,socketList).start();//数组传入，目的是要将信息发送到所有在线客户端
        }
        serverSocket.close(); //关闭ServerSocket
    }
}

